/**
 * @file readv.c
 * @author lyke (1556553295@qq.com)
 * @brief 向量IO readv() 示例代码
 * @version 0.1
 * @date 2023-01-30
 *
 * @copyright Copyright (c) 2023
 *
 */

#include <fcntl.h> // open()
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/uio.h> // vector I/O
#include <unistd.h> // close()

int main(int argc, char const *argv[]) {
  char foo[10], bar[10], baz[10];
  struct iovec iov[3];
  ssize_t nr;
  int fd;

  fd = open("recoder.txt", O_RDONLY);
  if (-1 == fd) {
    perror("open");
    return 1;
  }
  iov[0].iov_base = foo;
  iov[0].iov_len = sizeof(foo);
  iov[1].iov_base = bar;
  iov[1].iov_len = sizeof(bar);
  iov[2].iov_base = baz;
  iov[2].iov_len = sizeof(baz);

  nr = readv(fd, iov, 3);
  if (-1 == nr)
  {
    perror("readv");
    return 1;
  }
  

  for (size_t i = 0; i < 3; i++) {
    printf("%s %zu \n", (char *)iov[i].iov_base, iov[i].iov_len);
  }

  if (close(fd)) {
    perror("close");
    return 1;
  }

  return 0;
}
